/**
 * 
 */
/**
 * 
 */
let websocket = null;
let timeInterval;
if ('WebSocket' in window) {
	websocket = new WebSocket('ws://localhost:8082/socket/asset');
} else {
	alert('浏览器不支持');
}
window.onbeforeunload = function() {
	websocket.close();
};
let mlocation;
let mVue = new Vue({
	el: '#mSpace',
	data: {
		username: 'admin',
		collapse: false,
		fullscreen: false,
		name: 'linxin',
		messageCount:0,
		//message: parseInt(sessionStorage.getItem("messageCount")),
		//messageText: sessionStorage.getItem("messageText"),
		hrList: [
			{
				hid: "hr2",
				bpro: "北京",
				hpwd: "$2a$10$rzFWC6Tkm2DidDhhGyLvC.cbqmh66u.2bNpwn1uqVuV2yezRcCFlC",
				hlevel: -1,
				hgender: "男",
				bdetails: "辉瑞公司为美资公司，近10年来全球制药行业排名基本前2位，目前在国内是的制药公司，经常被称为宇宙药厂。辉瑞公司的产品覆盖了包括化学药物、生物制剂、疫苗、健康药物等诸多广泛而极具潜力的治疗及健康领域，同时其卓越的研发和生产能力处于全球领先地位。",
				batchid: 4,
				hemail: "778722@qq.com",
				bimg: "static\\img\\ruihui.jpg\r\n",
				number: 2546,
				hjobname: "人力资源",
				bname: "辉瑞",
				hphone: "17855549032",
				baddress: "辉瑞投资有限公司",
				bid: 3,
				bcity: "北京",
				status: 1
			},
		],
		showdiv: [
			{ isshow: true },
			{ isshow: false },
			{ isshow: false },
			{ isshow: false },
			{ isshow: false },
		],
		userDtable: [
			{ isM: true, isEnable: false },
			{ isM: true, isEnable: false },
			{ isM: true, isEnable: false },
			{ isM: true, isEnable: false },
			{ isM: true, isEnable: false },
			{ isM: true, isEnable: false },
		],
		addClass: false,
		class1: 'el-icon-refresh refresh',
		class2: '',
		test: [{ totalBussiness: {}, totalUnreviewed: {} }],
		message:'',
		unrecivedB: [],
		name: localStorage.getItem('ms_username'),
		logoImg: 'uimg/20201220/47d7110013944246bbd858885e9ba6a2.jpg',
		bTotal: '',
		todoList: [
		],
		data: [
		],
		userData: [],
		jobData: [],
		welfare: [],
		tableData: [],
		options2: {
			type: 'line',
			title: {
				text: '最近几个月各品类销售趋势图'
			},
			labels: ['6月', '7月', '8月', '9月', '10月'],
			datasets: [
				{
					label: '家电',
					data: [234, 278, 270, 190, 230]
				},
				{
					label: '百货',
					data: [164, 178, 150, 135, 160]
				},
				{
					label: '食品',
					data: [74, 118, 200, 235, 90]
				}
			]
		}

	},
	created() {
		this.websocketinit();
		this.getAllU();
		this.getAllJ();
		this.getAllH();
		this.getAllB();
	},
	destroyed() {
		clearInterval(timeInterval);
	},
	computed: {
		role() {
			return this.name === 'admin' ? '管理员' : '普通用户';
		}
	},
	methods: {
		logout(){
			let logoutBtn = document.getElementById("mlogout");
			logoutBtn.click();
		},
		deleteJob(jid) {


			this.$confirm('此操作将永久删除该职位, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let data = new FormData();
				console.log(jid);
				data.append("jid", jid);
				axios.post('/manager/deleteJob', data).then((resp) => {
					if (resp.data > 0)
						this.getAllJ();
					this.$message.success("删除成功");
				}).catch(error => { console.log("error" + error) });
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});


		},
		reject(buss) {
			let data = new FormData();
			data.append("bid", buss.bid);
			axios.post('/manager/disOneB', data).then((resp) => {
				if (resp.data > 0)
					this.getAllB();
			}).catch(error => { console.log("error" + error) });
		},
		disBuss(buss) {
			if (buss.status === 2) {
				let data = new FormData();
				data.append("bid", buss.bid);
				axios.post('/manager/agreeOneB', data).then((resp) => {
					if (resp.data > 0)
						this.getAllB();
				}).catch(error => { console.log("error" + error) });
			}
			if (buss.status === 1) {
				let data = new FormData();
				data.append("bid", buss.bid);
				axios.post('/manager/disOneB', data).then((resp) => {
					if (resp.data > 0)
						this.getAllB();
				}).catch(error => { console.log("error" + error) });
			}

		},
		checkOneBuss(bname) {
			sessionStorage.setItem("bname", bname);
			window.location.href = "http://localhost:8082/usert/b";
		},
		getAllB() {
			axios.post('/manager/getAllB').then(success => {
				this.tableData = success.data;
				//console.log(this.tableData);
			}).catch(error => { console.log("error" + error) });
		},
		getAllH() {
			axios.post('/manager/getAllH').then(success => {
				this.hrList = success.data;
				//console.log(this.hrList);
			}).catch(error => { console.log("error" + error) });
		},
		getAllU() {
			axios.post('/usert/getAll').then(success => {
				this.userData = success.data;
			});
		},
		getAllJ() {
			axios.post('/job/getAll').then(success => {
				this.jobData = success.data;
			});
		},
		disabled(uid) {

			this.$confirm('此操作将禁用该用户, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let data = new FormData();
				data.append("uid", uid);
				axios.post('/manager/disabledU', data)
					.then(success => {
						if (success.data === 200) {
							this.$message.success("修改成功");
							this.getAllU();
						}

						else
							this.$message.error("修改失败");
					})
					.catch();
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});


		},
		enabled(uid) {
			this.$confirm('此操作将解禁该用户, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {

				let data = new FormData();
				data.append("uid", uid);
				axios.post('/manager/enabledU', data)
					.then(success => {
						if (success.data === 200) {
							this.$message.success("修改成功");
							this.getAllU();
						}
						else
							this.$message.error("修改失败");
					})
					.catch();

			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消'
				});
			});
		},
		deleteU(uid) {

			this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let data = new FormData();
				data.append("uid", uid);
				axios.post('/manager/deleteU', data)
					.then(success => {
						if (success.data === '删除失败，请重试')
							this.$message.error(success.data);
						else
							this.$message.success(success.data);
						this.getAllU();
					}).catch(error => {
						this.$message.error("删除失败");
					});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});

			//	axios.baseUrl = 'http://localhost:8082'

		},
		resetDateFilter() {
			this.$refs.filterTable.clearFilter('date');
		},
		clearFilter() {
			this.$refs.filterTable.clearFilter();
		},
		formatter(row, column) {
			return row.uemail;
		},
		filterTag(value, row) {
			return row.tag === value;
		},
		filterHandler(value, row, column) {
			const property = column['property'];
			return row[property] === value;
		},
		show(index) {
			for (let i = 0; i < this.showdiv.length; i++) {
				if (i === index) {
					this.showdiv[i].isshow = true;
				}
				else {
					this.showdiv[i].isshow = false;
				}

			}

		},
		changeDate() {
			const now = new Date().getTime();
			this.data.forEach((item, index) => {
				const date = new Date(now - (6 - index) * 86400000);
				item.name = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
			});
		},
		websocketinit() {
			let _this = this;
			websocket.onopen = function(event) {
				console.log('建立连接');
				websocket.send('消息发送测试（客户端）');
			};
			websocket.onclose = function(event) {
				console.log('连接关闭');
			};

			websocket.onerror = function() {
				console.log('error');
			};
			websocket.onmessage = function(event) {
				let data = JSON.parse(event.data);
					_this.test = JSON.parse(event.data);
					_this.message = _this.test[2].headmessage;
					_this.getAllB();
					_this.getAllU();
					_this.getAllH();
					_this.getAllJ();
					if(_this.message!==''){
						_this.messageCount++;
					}
				console.log("实时消息"+event.data);
				//_this.unrecivedB = JSON.parse(event.data[1]);
				//console.log(_this.test);
				//_this.test = parseInt(event.data);
				//  console.log('收到消息::' + event.data+_this.test);
				//在此写收到消息后的操作
				//websocket.send("收到数据");
			};
		},
		freshTotal() {
			let myThis = this;
			myThis.addClass = true;
			clearInterval(timeInterval);
			myThis.class2 = 'el-icon-refresh refresh transform';
			websocket.send('请求刷新');
			websocket.onmessage = function(event) {
				myThis.test = JSON.parse(event.data);
			};
			timeInterval = setInterval(function() {
				myThis.addClass = false;
			}, 1000);
		},
		handleCommand(command) {
			if (command == 'loginout') {
				localStorage.removeItem('ms_username');
				this.$router.push('/login');
			}
		},
		// 侧边栏折叠
		collapseChage() {
			this.collapse = !this.collapse;
			bus.$emit('collapse', this.collapse);
		},
	}
});